package com.foreverwin.jzymes.production.labor.service.impl;

import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.labor.domain.LaborAttendanceLog;
import com.foreverwin.jzymes.production.labor.mapper.laborSelectLogMapper;
import com.foreverwin.jzymes.production.labor.service.ILaborAttendSerchService;
import com.foreverwin.jzymes.production.labor.service.ILaborAttendanceLogService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.enums.ProcedureEnum;
import com.foreverwin.jzymes.production.sfc.mapper.SFCAdviceMapper;
import org.springframework.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LaborAttendSerchServiceImpl implements ILaborAttendSerchService {
    @Autowired
    ILaborAttendanceLogService logService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IFieldGroupAdviceService fieldGroupAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    SFCAdviceMapper sfcAdviceMapper;
    @Autowired
    laborSelectLogMapper laborSelectLogMapper;
    @Autowired
    IProductionShiftService productionShiftService;

    /**
     * 新增人力出勤相关信息
     * @param  attendanceLog
     */
    @Override
    public void insertLaborAttend(LaborAttendanceLog attendanceLog) {
        /**校验*/
        checkLaborData(attendanceLog);
        /**获取机种*/
        String itemgroup = itemGroupAdviceService.findItemGroupBySite( attendanceLog.getSite() ) ;
        /**获取机种成员*/
        List<Map<String,Object>> itemGroupMembers = itemGroupAdviceService.findItemGroupMember( attendanceLog.getSite() , itemgroup );

        attendanceLog.setMachineType( itemgroup );
        Long labordiff =  attendanceLog.getStdLabor() - attendanceLog.getActualLabor();
        attendanceLog.setLaborDiff( labordiff );

        /**记录四个料件的人力出勤信息*/
        for ( Map<String,Object> map : itemGroupMembers  ){

            /**班次、日期对应的时间段*/
            Date[] dates =  productionShiftService.findShiftRange( attendanceLog.getSite() , attendanceLog.getShift() , attendanceLog.getWorkDate() );

            String itemBo =  ( String ) map.get("ITEM_BO");
            List<SFC> sfcLists =laborSelectLogMapper.selectSFCBytime( attendanceLog.getSite(),dates[1],itemBo,attendanceLog.getProcedureBo() );
            Assert.isTrue( sfcLists != null && sfcLists.size() > 0, I18nUtil.getI18nText( "common.sfcNotBlank" ) );
            SFC sfc = sfcLists.get( 0 );
            /**工令*/
         /*  List<SFC> sfcList =  sfcAdviceMapper.selectSFCListOnWork( attendanceLog.getSite() , attendanceLog.getProcedureBo() ,itemBo );
           SFC sfc = sfcList.get( 0 );*/

           /**填充字段*/
           attendanceLog.setSfc( sfc.getSfc() );
           attendanceLog.setQtyPlan( sfc.getPlanQty() );
           attendanceLog.setQtyDone( sfc.getSfcQty() );
           attendanceLog.setItemBo( itemBo );
           attendanceLog.setStdIndvlYield( attendanceLog.getQtyPlan()/attendanceLog.getStdLabor() );
           String handle = BOHandleUtil.buildLaborAttendance( attendanceLog.getSite() , attendanceLog.getWorkDate() , attendanceLog.getProcedureBo() ,attendanceLog.getItemBo() , attendanceLog.getDept() ,attendanceLog.getShift() );
           attendanceLog.setHandle( handle );
           attendanceLog.setCreatedDateTime( new Date() );
          // attendanceLog.setCreateUserBo( CommonMethods.getUserBo() );

           /**制程*/
           String procedure = fieldGroupAdviceService.getFieldName( sfc.getProcedureBo() );
           /**DDG实际人均产出*/
           if ( ProcedureEnum.GRIND == ProcedureEnum.valueOf( procedure ) ){

             Long  actual = laborSelectLogMapper.selectLaborattendanceDDG( attendanceLog.getSite(),dates[0] , dates[1] , itemBo );

             if ( actual == null ){
                 attendanceLog.setActualIndvlYield( 0l );
             }else {

                 attendanceLog.setActualIndvlYield( actual/attendanceLog.getActualLabor() );
             }

            }

            /**CNC实际人均产出*/
            if ( ProcedureEnum.CNC == ProcedureEnum.valueOf( procedure ) ){

              Long  actual = laborSelectLogMapper.selectLaborattendanceCNC( attendanceLog.getSite(),dates[0] , dates[1] , itemBo );

                if ( actual == null ){
                    attendanceLog.setActualIndvlYield( 0l );
                }else {

                    attendanceLog.setActualIndvlYield( actual/attendanceLog.getActualLabor() );
                }
            }

           /**人均产出差异*/
           attendanceLog.setIndvlYieldDiff( attendanceLog.getStdIndvlYield() - attendanceLog.getActualIndvlYield() );
           /**保存人力出勤记录*/
           LaborAttendanceLog laborAttendanceLog = logService.selectLaborAttendanceLogById( handle );

           if ( laborAttendanceLog == null ) {
               /**first*/
              logService.insertLaborAttendanceLog(attendanceLog);

           }else{

               /**update*/
               laborAttendanceLog.setStdLabor( attendanceLog.getStdLabor() );
               laborAttendanceLog.setActualLabor( attendanceLog.getActualLabor() );
               laborAttendanceLog.setLaborDiff( attendanceLog.getLaborDiff() );
               laborAttendanceLog.setStdIndvlYield( attendanceLog.getStdIndvlYield() );
               laborAttendanceLog.setActualIndvlYield( attendanceLog.getActualIndvlYield() );
               laborAttendanceLog.setIndvlYieldDiff( attendanceLog.getIndvlYieldDiff() );
               laborAttendanceLog.setUpdatedDateTime( new Date() );
               laborAttendanceLog.setUpdateUserBo( attendanceLog.getCreateUserBo() );

               logService.updateLaborAttendanceLog( laborAttendanceLog );
           }
        }

    }

    /**
     * 对前台提交传入的数据进行校验
     * @param  attendanceLog
     */
    public  void  checkLaborData(LaborAttendanceLog attendanceLog){
        /**校验站点*/
        siteAdviceService.existSite( attendanceLog.getSite() );
        /**制程不能为空*/
        fieldGroupAdviceService.existFieldGroup( attendanceLog.getProcedureBo() );
        /**部门不能为空*/
        fieldGroupAdviceService.existFieldGroup( attendanceLog.getDept() );
        /**出勤时间不能为空*/
        Assert.notNull( attendanceLog.getWorkDate(), I18nUtil.getI18nText("production.labor.workdate") );
        /**班次不能为空*/
        Assert.notNull( attendanceLog.getShift(), I18nUtil.getI18nText("production.labor.shift") );
        /**标准人力不能为空*/
        Assert.notNull( attendanceLog.getDept(), I18nUtil.getI18nText("production.labor.dept") );
        /**实际人力不能为空*/
        Assert.notNull( attendanceLog.getDept(), I18nUtil.getI18nText("production.labor.dept") );
    }
}
